/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nhadat.xml;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;
import nhadat.common.CommonConstant;
import nhadat.dao.KhuVucDAO;
import nhadat.dao.LoaiGiayToDAO;
import nhadat.dao.LoaiNhaDAO;
import nhadat.dao.LoaiTinDAO;
import nhadat.dao.TindangDao;
import nhadat.entity.TblKhuVuc;
import nhadat.entity.TblLoaiGiayTo;
import nhadat.entity.TblLoaiNha;
import nhadat.entity.TblLoaiTin;
import nhadat.entity.TblNguoiDung;
import nhadat.entity.TblTinDang;
import nhadat.tindangfull.TindangfullType;

/**
 *
 * @author Xe Bay
 */
public class DatabaseAndXMLFileMappingSyncrolization {

    XMLmanipulate createXML;

    public DatabaseAndXMLFileMappingSyncrolization() {
        createXML = new XMLmanipulate();
    }

    public void getAllTinDangToXML(String outputXML) {
        TindangDao tinDao = new TindangDao();
        TblTinDang[] dstindang = tinDao.getAllTindang();
        createXML.createXMLfromTblTinDang(dstindang, outputXML);
    }

    public void getAllLoaiTinToXML(String ouputXML) {
        createXML.createXMLfromTblLoaiTin(ouputXML);
    }

    public void getAllKhuVucToXML(String outputXML) {
        createXML.createXMLfromTblKhuvuc(outputXML);
    }

    public void getAllLoaiNhaToXML(String outputXML) {
        createXML.createXMLfromTblLoaiNha(outputXML);
    }

    public void getAllLoaiGiayToToXML(String outputXML) {
        createXML.createXMLfromTblLoaiGiayTo(outputXML);
    }

    public void createBLANK_TinDangFullXML(String outputXML) {
        createXML.createBLANK_TinDangFullXML(outputXML);
    }

    public int getMaTinDangLonNhat() {
        TindangDao tindangDAO = new TindangDao();
        return tindangDAO.getMaTinDangLonNhat();
    }

    public void createFullDsKhuVuc(String outputXML) {
        KhuVucDAO khuvucDAO = new KhuVucDAO();
        TblKhuVuc[] dskhuvuc = khuvucDAO.getAllKhuVuc();
        createXML.createXMLfromTblKhuVuc(dskhuvuc, outputXML);
    }

    public void createFullDsLoaiGiayTo(String outputXML) {
        LoaiGiayToDAO gtDao = new LoaiGiayToDAO();
        TblLoaiGiayTo[] dsGiayto = gtDao.getAllLoaiGiayTo();
        createXML.createXMLfromTblLoaiGiayTo(dsGiayto, outputXML);
    }

    public void createFullDsLoaiTin(String outputXML) {
        LoaiTinDAO tinDao = new LoaiTinDAO();
        TblLoaiTin[] dsLoaitin = tinDao.getAllLoaiTin();
        createXML.createXMLfromTblLoaiTin(dsLoaitin, outputXML);
    }

    public void createFullDsLoaiNha(String outputXML) {
        LoaiNhaDAO nhaDao = new LoaiNhaDAO();
        TblLoaiNha[] dsloainha = nhaDao.getAllLoaiNha();
        createXML.createXMLfromTblLoaiNha(dsloainha, outputXML);
    }

    /**
     *  Insert - Update - Delete Tindang to database,  base TindangFull.xml
     * @param inputXML
     */
    public void InsertUpdateDelete(String inputTindangFullXML,
            String inputLoaiNhaXML, String inputLoaiGiayToXML, String inputKhuVucXML,
            String inputLoaiTinXML) {
        StaxParser reader = new StaxParser();
        ArrayList<TindangfullType> listTinDang = reader.readXMLtoTindangFullTypebyStAX(inputTindangFullXML);
        TindangfullType[] dsTindang = new TindangfullType[listTinDang.size()];
        dsTindang = listTinDang.toArray(dsTindang);



        for (int i = 0; i < dsTindang.length; i++) {
            TindangfullType tindangfullType = dsTindang[i];
            DatabaseAndXMLFileMappingSyncrolization map = new DatabaseAndXMLFileMappingSyncrolization();
            //Mapping with object TblTindang of hibernate
            TblTinDang TindangFull = map.mapTblTinDangWithTindangFullType(tindangfullType, inputLoaiNhaXML,
                    inputLoaiGiayToXML, inputKhuVucXML, inputLoaiTinXML);

            if (tindangfullType.getStatus().equals("new")) {
                TindangDao tindang = new TindangDao();
                // Insert new tindang to database using hibernate
                tindang.insertTindang(TindangFull);

            } else if (tindangfullType.getStatus().equals("update")) {
                TindangDao tindang = new TindangDao();
                //Update tindang to database using hibernate, base matindang
                tindang.updateTindang(TindangFull, TindangFull.getMatindang());

            } else if (tindangfullType.getStatus().equals("delete")) {
                TindangDao tindang = new TindangDao();
                // Delete tindang to database using hibernate, base matindang
                tindang.deleteTindang(TindangFull.getMatindang());
            }

        }
    }

    /**
     * Mapping from tindangFullType to TblTindang
     * @param tindangfull
     * @param inputLoaiNhaXML
     * @param inputLoaiGiayToXML
     * @param inputKhuVucXML
     * @param inputLoaiTinXML
     * @return
     */
    public TblTinDang mapTblTinDangWithTindangFullType(TindangfullType tindangfull,
            String inputLoaiNhaXML, String inputLoaiGiayToXML, String inputKhuVucXML,
            String inputLoaiTinXML) {
        try {
            TblTinDang tindangDB = new TblTinDang();
            StaxParser readXML = new StaxParser();
            String expression = "";
            //String inputXML = "";

            tindangDB.setMatindang(Integer.parseInt(tindangfull.getMatindang()));
            //Mapping maloaitin
            expression = "//dsloaitin/loaitin[tenloaitin='" + tindangfull.getTenloaitin()
                    + "']/maloaitin";
            //inputXML = "web/xml/TblLoaiNha.xml";
            String maloaitin = readXML.readXMLgetMaByTen(expression, inputLoaiTinXML);
            TblLoaiTin loaitin = new TblLoaiTin();
            loaitin.setMaloaitin(maloaitin);
            loaitin.setTenloaitin(tindangfull.getTenloaitin());

            tindangDB.setTblLoaiTin(loaitin);


            //Mapping maloainha
            expression = "//dsloainha/loainha[tenloainha='" + tindangfull.getTenloainha()
                    + "']/maloainha";
            //inputXML = "web/xml/TblLoaiNha.xml";
            String maloainha = readXML.readXMLgetMaByTen(expression, inputLoaiNhaXML);
            TblLoaiNha loainha = new TblLoaiNha();
            loainha.setMaloainha(maloainha);
            loainha.setTenloainha(tindangfull.getTenloainha());

            tindangDB.setTblLoaiNha(loainha);

            //Mapping maloaigiayto
            expression = "//dsloaigiayto/loaigiayto[tenloaigiayto='" + tindangfull.getTenloaigiayto()
                    + "']/maloaigiayto";
            //inputXML = "web/xml/TblLoaiGiayTo.xml";
            String maloaigiayto = readXML.readXMLgetMaByTen(expression, inputLoaiGiayToXML);
            TblLoaiGiayTo loaigiayto = new TblLoaiGiayTo();
            loaigiayto.setMaloaigiayto(maloaigiayto);
            loaigiayto.setTenloaigiayto(tindangfull.getTenloaigiayto());
            tindangDB.setTblLoaiGiayTo(loaigiayto);

            //Mapping thongtinchitiet
            tindangDB.setThongtinchitiet(tindangfull.getThongtinchitiet());

            //Mapping cautruc
            tindangDB.setCautruc(tindangfull.getCautruc());

            //Mapping dientichkhuonvien
            tindangDB.setDientichkhuonvien(tindangfull.getDientichkhuonvien().getSolieu());
            tindangDB.setDvddientichkhuonvien(tindangfull.getDientichkhuonvien().getDvddientichkhuonvien());

            //Mapping chieungangkhuonvien
            tindangDB.setChieungangkhuonvien(tindangfull.getChieungangkhuonvien().getSolieu());
            tindangDB.setDvdchieungangkhuonvien(tindangfull.getChieungangkhuonvien().getDvdchieungangkhuonvien());

            //Mapping chieudaikhuonvien
            tindangDB.setChieudaikhuonvien(tindangfull.getChieudaikhuonvien().getSolieu());
            tindangDB.setDvdchieudaikhuonvien(tindangfull.getChieudaikhuonvien().getDvdchieudaikhuonvien());

            //Mapping dientichxaydung
            tindangDB.setDientichxaydung(tindangfull.getDientichxaydung().getSolieu());
            tindangDB.setDvddientichxaydung(tindangfull.getDientichxaydung().getDvddientichxaydung());

            //Mapping chieungangxaydung
            tindangDB.setChieungangxaydung(tindangfull.getChieungangxaydung().getSolieu());
            tindangDB.setDvdchieungangxaydung(tindangfull.getChieungangxaydung().getDvdchieungangxaydung());

            //Mappung chieudaixaydung
            tindangDB.setChieudaixaydung(tindangfull.getChieudaixaydung().getSolieu());
            tindangDB.setDvdchieudaixaydung(tindangfull.getChieudaixaydung().getDvdchieudaixaydung());

            //Mapping diachi
            tindangDB.setDiachi(tindangfull.getDiachi());

            //Mapping makhuvuc
            expression = "//dskhuvuc/khuvuc[tinhthanh='" + tindangfull.getKhuvuc().getTinhthanh()
                    + "' and quanhuyen='" + tindangfull.getKhuvuc().getQuanhuyen() + "']/makhuvuc";
            //inputXML = "web/xml/TblKhuVuc.xml";
            String makhuvuc = readXML.readXMLgetMaByTen(expression, inputKhuVucXML);
            TblKhuVuc khuvuc = new TblKhuVuc();
            khuvuc.setMakhuvuc(makhuvuc);
            khuvuc.setQuanhuyen(tindangfull.getKhuvuc().getQuanhuyen());
            khuvuc.setTinhthanh(tindangfull.getKhuvuc().getTinhthanh());
            tindangDB.setTblKhuVuc(khuvuc);

            //Mapping giatien
            tindangDB.setGiatien(tindangfull.getGiatien().getSolieu());
            tindangDB.setDvdgiatien(tindangfull.getGiatien().getDvdgiatien());

            //Mapping hinhanh
            tindangDB.setHinhanh(tindangfull.getHinhanh());

            //Mapping tieudetin
            tindangDB.setTieudetin(tindangfull.getTieudetin());



            //Mapping nguoidangtin
            TblNguoiDung user = new TblNguoiDung();
            user.setTendangnhap(tindangfull.getNguoidangtin());
            tindangDB.setTblNguoiDung(user);

            //Mapping ngaydangtin
            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                date = formatter.parse(tindangfull.getNgaydangtin());
            } catch (ParseException ex) {
                Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
            }
            tindangDB.setNgaydangtin(date);
            try {
                date = formatter.parse(tindangfull.getLancuoicapnhat());
            } catch (ParseException ex) {
                Logger.getLogger(XMLmanipulate.class.getName()).log(Level.SEVERE, null, ex);
            }
            // Mapping lancuoicapnhat
            tindangDB.setLancuoicapnhat(date);

            //Mapping soluotxem
            tindangDB.setSoluotxem(Integer.parseInt(tindangfull.getSoluotxem().toString()));

            //Mapping isActive
            tindangDB.setIsActived(Boolean.parseBoolean(tindangfull.getIsActive()));

            return tindangDB;

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }

        return null;
    }

    public void createPDFReportForAdmin(String xmlPath, String xslPath, String foPath, String outputFile) {
        XMLReportLogic xmlLogic = new XMLReportLogic();

        String[] location = xmlLogic.generateTheLocationOfReport();
        //outputFile += "/" + location[0];
        //foPath += "/" + location[0];
        outputFile += "/report-" + location[1] + ".pdf";
        //foPath += "/report-" + location[1] + ".fo";
        //xmlLogic.methodTrAX(xmlPath, xslPath, outputFile);
        xmlLogic.createReportXMLFO(xmlPath, xslPath, foPath, outputFile);
    }

     public void createBLANK_ReportXML(String outputXML) {
        XMLReportLogic xmlLogic = new XMLReportLogic();
        xmlLogic.createBLANK_ReportXML(outputXML);
    }
}
